package com.chinabank.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.chinabank.dao.AccountDao;
import com.chinabank.exception.NoSuchAccountException;
import com.chinabank.model.Account;


@Repository(value = "accountDao")
public class AccountDaoImpl extends BaseDaoImpl<Account> implements AccountDao {

	@Override
	public Account get(String username, String password)
			throws NoSuchAccountException {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = "from Account as account where account.username=? and account.password=?";
		List<Account> lst = (List<Account>) session.createQuery(hql)
				.setString(0, username).setString(1, password).list();
		if (lst.size() < 1) {
			throw new NoSuchAccountException(username);
		}
		return lst.get(0);
	}

	@Override
	public List<Account> listAccount() {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = "from Account";
		List<Account> lst = (List<Account>) session.createQuery(hql).list();
		return lst;
	}

	@Override
	public void delAccount(Serializable id) {
		Session session = this.getSessionFactory().getCurrentSession();
		Account account = (Account) session.get(Account.class, id);
		session.delete(account);
	}

}
